package org.soarsky.wealthchengdu.network;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.soarsky.wealthchengdu.entity.CommentResultMsg;
import org.soarsky.wealthchengdu.entity.FocusData;
import org.soarsky.wealthchengdu.entity.JsonResult;
import org.soarsky.wealthchengdu.entity.LogonResult;
import org.soarsky.wealthchengdu.entity.NewsContentData;
import org.soarsky.wealthchengdu.entity.NewsResultMsg;
import org.soarsky.wealthchengdu.entity.PublicRequest;
import org.soarsky.wealthchengdu.entity.WeatherData;
import org.soarsky.wealthchengdu.tools.LengthUtils;
import org.soarsky.wealthchengdu.tools.ParserJsonFactory;
import org.soarsky.wealthchengdu.tools.ParserJsonFactory.ResponseCode;
import org.soarsky.wealthchengdu.util.Constants;


/**
 * 数据解析结果集
 *
 */
/**
 * @author Administrator
 * 返回结果说明JsonResult<T> result;
 * 1、result=null; 说明无网络数据
 * 2、result.isError()  查看数据是否有错误
 * 3、result.getData()  返回数据结果集
 * 4、 Exception  解析失败异常
 */
public class ParserResults implements FGFWebService {

	/**
	 * 是否有错误信息
	 * */
	private boolean isError(JsonResult<?> result, JSONObject jsonObject)
			throws Exception {
		if (!jsonObject.has("code")) {
			return false;
		}
		result.setError(true);
		result.setCode(jsonObject.getString("code"));
		result.setMsg(jsonObject.getString("msg"));
		return true;
	}

	// 设置公共属性
	private List<NameValuePair> getAttributes(String api) {
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		PublicRequest publicRequest = PublicRequest.newPublicRequest();
		publicRequest.setApi(api);
		publicRequest.setAttribute(list);
		return list;
	}

	// 获取结果集
	private ResponseCode<String> getResult(List<NameValuePair> list)
			throws Exception {
		ResponseCode<String> msg = ParserJsonFactory.parserJsonHeadPost(
				Constants.ROOT, list);
		return msg;
	}

	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#authLogon(java.lang.String)
	 */
	@Override
	public JsonResult<LogonResult> authLogon(String userName) throws Exception {
		List<NameValuePair> list = getAttributes(Constants.AUTH_LOGIN);
		list.add(new BasicNameValuePair("userName", userName));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JsonResult<LogonResult> jsonResult = new JsonResult<LogonResult>();
		JSONObject object = new JSONObject(msg.getResData());
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		LogonResult result = new LogonResult();
		object = object.getJSONObject("results");
		ParserJsonFactory.parseAnnotationJsonData(object, result, false);
		jsonResult.setData(result);
		PublicRequest.newPublicRequest().setSessionKey(result.getSessionKey());
		return jsonResult;
	}

	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#authLogout()
	 */
	@Override
	public JsonResult<LogonResult> authLogout() throws Exception {
		List<NameValuePair> list = getAttributes(Constants.AUTH_LOGOUT);
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JsonResult<LogonResult> jsonResult = new JsonResult<LogonResult>();
		JSONObject object = new JSONObject(msg.getResData());
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		LogonResult result = new LogonResult();
		object = object.getJSONObject("results");
		ParserJsonFactory.parseAnnotationJsonData(object, result, false);
		jsonResult.setData(result);
		return jsonResult;
	}

	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#updNickName(java.lang.String)
	 */
	@Override
	public JsonResult<Boolean> updNickName(String nickName) throws Exception {
		List<NameValuePair> list = getAttributes(Constants.AUTH_UPNICKNAME);
		list.add(new BasicNameValuePair("nickName", nickName));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JsonResult<Boolean> jsonResult = new JsonResult<Boolean>();
		JSONObject object = new JSONObject(msg.getResData());
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		object = object.getJSONObject("results");
		boolean results = object.getBoolean("result");
		jsonResult.setData(results);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getFocus(java.lang.String)
	 */
	@Override
	public JsonResult<List<FocusData>> getFocus(String cateId) throws Exception{
		return getFocus(cateId, null, "3", "1");
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getFocus(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public JsonResult<List<FocusData>> getFocus(String cateId,String limit,String order,String sort) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.FOCUS_GET);
		list.add(new BasicNameValuePair("cateId", cateId));
		if(limit!=null)
			list.add(new BasicNameValuePair("limit", limit));
		if(order!=null)
			list.add(new BasicNameValuePair("order", order));
		if(sort!=null)
			list.add(new BasicNameValuePair("sort", sort));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JsonResult<List<FocusData>> jsonResult = new JsonResult<List<FocusData>>();
		JSONObject object = new JSONObject(msg.getResData());
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		List<FocusData> flist = new ArrayList<FocusData>();
		JSONArray array = object.getJSONArray("results");
		int len=array.length();
		FocusData data;
		for(int i=0;i<len;i++){
			data = new FocusData();
			object=array.getJSONObject(i);
			ParserJsonFactory.parseAnnotationJsonData(object, data, true);
			flist.add(data);
		}
		jsonResult.setData(flist);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getNews(java.lang.String, java.lang.String)
	 */
	@Override
	public JsonResult<NewsResultMsg> getNews(String cateId,String level) throws Exception{
		return getNews(cateId, level, null, null, null, null, null);
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getNews(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public JsonResult<NewsResultMsg> getNews(String cateId,String level,String keyword,String limit,String page,String order,String sort) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.NEWS_GET_LIST);
		if(LengthUtils.isEmpty(cateId)||LengthUtils.isEmpty(level))
			throw new RuntimeException("cateId和level不能为空");
		list.add(new BasicNameValuePair("cateId", cateId));
		list.add(new BasicNameValuePair("level", level));
		if(keyword!=null)
			list.add(new BasicNameValuePair("keyword", keyword));
		if(limit!=null)
			list.add(new BasicNameValuePair("limit", limit));
		if(page!=null)
			list.add(new BasicNameValuePair("page", page));
		if(order!=null)
			list.add(new BasicNameValuePair("order", order));
		if(sort!=null)
			list.add(new BasicNameValuePair("sort", sort));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<NewsResultMsg> jsonResult = new JsonResult<NewsResultMsg>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		object = object.getJSONObject("results");
		NewsResultMsg data = new NewsResultMsg();
		ParserJsonFactory.parseAnnotationJsonData(object, data, true);
		jsonResult.setData(data);
		return jsonResult;
	}
	

	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getNewsContent(java.lang.String)
	 */
	@Override
	public JsonResult<NewsContentData> getNewsContent(String id) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.NEWS_GET_CONTENT);
		if(LengthUtils.isEmpty(id))
			throw new RuntimeException("id不能为空");
		list.add(new BasicNameValuePair("id", id));
		ResponseCode<String> msg = getResult(list);
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<NewsContentData> jsonResult = new JsonResult<NewsContentData>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		object = object.getJSONObject("results");
		NewsContentData data = new NewsContentData();
		ParserJsonFactory.parseAnnotationJsonData(object, data, true);
		jsonResult.setData(data);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getComment(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public JsonResult<CommentResultMsg> getComment(String id,String limit,String page) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.COMMENT_GET_LIST);
		if(LengthUtils.isEmpty(id))
			throw new RuntimeException("id不能为空");
		list.add(new BasicNameValuePair("id", id));
		if(limit!=null)
			list.add(new BasicNameValuePair("limit", limit));
		if(page!=null)
			list.add(new BasicNameValuePair("page", page));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<CommentResultMsg> jsonResult = new JsonResult<CommentResultMsg>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		object = object.getJSONObject("results");
		CommentResultMsg data = new CommentResultMsg();
		ParserJsonFactory.parseAnnotationJsonData(object, data, true);
		jsonResult.setData(data);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#sendComment(java.lang.String, java.lang.String)
	 */
	@Override
	public JsonResult<String> sendComment(String id,String content) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.COMMENT_ADD);
		if(LengthUtils.isEmpty(id)||LengthUtils.isEmpty(content))
			throw new RuntimeException("id和content不能为空");
		list.add(new BasicNameValuePair("id", id));
		list.add(new BasicNameValuePair("content", content));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<String> jsonResult = new JsonResult<String>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		String comment=object.getJSONObject("results").getString("comment");
		jsonResult.setData(comment);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#sendVote(java.lang.String)
	 */
	@Override
	public JsonResult<String> sendVote(String id) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.VOTE_ADD);
		if(LengthUtils.isEmpty(id))
			throw new RuntimeException("id不能为空");
		list.add(new BasicNameValuePair("id", id));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<String> jsonResult = new JsonResult<String>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		String comment=object.getJSONObject("results").getString("vote");
		jsonResult.setData(comment);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#sendShare(java.lang.String)
	 */
	@Override
	public JsonResult<String> sendShare(String id) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.SHARE_ADD);
		if(LengthUtils.isEmpty(id))
			throw new RuntimeException("id不能为空");
		list.add(new BasicNameValuePair("id", id));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<String> jsonResult = new JsonResult<String>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		String comment=object.getJSONObject("results").getString("share");
		jsonResult.setData(comment);
		return jsonResult;
	}
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#sendCollect(java.lang.String)
	 */
	@Override
	public JsonResult<String> sendCollect(String id) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.COLLECT);
		if(LengthUtils.isEmpty(id))
			throw new RuntimeException("id不能为空");
		list.add(new BasicNameValuePair("id", id));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<String> jsonResult = new JsonResult<String>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		String comment=object.getJSONObject("results").getString("collect");
		jsonResult.setData(comment);
		return jsonResult;
	}
	
	/* (non-Javadoc)
	 * @see com.example.httpget.FGFWebService#getWeather(java.lang.String)
	 */
	@Override
	public JsonResult<WeatherData> getWeather(String limit) throws Exception{
		List<NameValuePair> list = getAttributes(Constants.WEATHER_GET);
		if(LengthUtils.isEmpty(limit))
			throw new RuntimeException("limit不能为空");
		list.add(new BasicNameValuePair("limit", limit));
		ResponseCode<String> msg = getResult(list);
		if (!msg.haveData())
			return null;
		JSONObject object = new JSONObject(msg.getResData());
		JsonResult<WeatherData> jsonResult = new JsonResult<WeatherData>();
		isError(jsonResult, object);
		if (jsonResult.isError())
			return jsonResult;
		
		WeatherData data = new WeatherData();
		object=object.getJSONObject("results").getJSONObject("weatherinfo");
		ParserJsonFactory.parseAnnotationJsonData(object, data, false);
		jsonResult.setData(data);
		return jsonResult;
	}
}
